<template>
  <div class="vehicle-details-wrap">
    <div class="box">
      <TopFilter ref="searchOrdersPanel" :list="searchOrdersList" :isSubmitBtn="true" :cols="4" :label-width="90" @filterChange="changeHandle" @onCollapse="collapseHandle" />
      <!--  :loading="addBtnState" -->
    </div>
    <FilterTable
      ref="table"
      columnsRef="part_report_partPrice"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :isSelectColumn="false"
      selectionType="multiple"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.onChangeColumn = arr)"
      :onSyncTableData="handleTableDataChange"
      :isExportExcel="true"
      :onCalcExportData="exportFun"
      exportFileName="配件价格显示.xlsx"
    >
      <template slot="controls">
        <!-- <el-button @click="openPayments" type="primary" >收 款</el-button> -->
      </template>
    </FilterTable>
  </div>
</template>

<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { getUserListSelect, queryPartPriceReport, getAllUser } from '@/api/part/report/partreport';
import { queryTex } from '@/api/part/common';
import { getSeries } from '@/api/common/cars/brandcarsModels';

export default {
  name: 'vehiclesDaily',
  components: {},
  mixins: [dictionary],
  data() {
    // this.dmsFunc = { dmsFuncId: 55100010 };
    this.arrayTypes = ['MULTIPLE_SELECT'];
    return {
      searchOrdersList: this.createSearchOrdersList(),
      columns: this.createTableColumns(),
      fetchParams: { xhrAbort: true },
      fetchApi: queryPartPriceReport,
      handleType: '',
      onChangeColumn: [],
      tableData: [], // table数据(手动计算含税价格)
      tax: 0, // 税率(计算含税价)
      seriesData: {} // 车系数据(翻译用)
    };
  },
  computed: {},
  watch: {},
  mounted() {
    // this.query()
    this.initSelect();
    this.getAllSeries();
  },
  methods: {
    exportFun(row) {
      for (const key in row) {
        switch (key) {
          case 'DISABLE_DATE':
            if (row[key]) {
              row[key] = moment(row[key]).format('YYYY-MM-DD');
            }
            break;
          case 'CONSULTANT':
            if (row[key]) {
              // row[key] = this.userList[row[key]];
            }
            break;
          case 'REFER_PRICE_TAXED':
            row[key] = Number(row['REFER_PRICE'] * (1 + (Number(this.tax) || 0.13))).toFixed(2);
            break;
          case 'PLAN_PRICE_TAXED':
            row[key] = Number((row.PLAN_PRICE ? row.PLAN_PRICE : 0) * (1 + Number(this.tax ? this.tax : 0.13))).toFixed(2);
            break;
          case 'COST_WHOLESALES_PRICE':
          case 'COST_OUT_PRICE':
          case 'COST_OUT_PRICE_A':
          case 'COST_OUT_PRICE_B':
          case 'COST_OUT_PRICE_C':
            row[key] = Number(row[key] ? row[key] : 0).toFixed(2);
            break;
          default:
            break;
        }
      }
    },
    async initSelect() {
      // 服务顾问
      const res = await getUserListSelect();
      const restech = await getAllUser();
    },
    createSearchOrdersList() {
      return [
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo',
          rules: [{ required: true, message: '请输入配件代码', trigger: 'change' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '加价率1',
          fieldName: 'addRate1'
        },
        {
          type: 'INPUT_NUMBER',
          label: '加价率2',
          fieldName: 'addRate2'
        },
        {
          type: 'INPUT_NUMBER',
          label: '加价率3',
          fieldName: 'addRate3'
        }
      ];
    },
    createTableColumns(queryType) {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 150,
          showOverflowTooltip: true
        },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 150,
          showOverflowTooltip: true
        },
        {
          title: '采购单价（含税）',
          dataIndex: 'PLAN_PRICE_TAXED',
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2
        },
        {
          title: '含税参考零售价',
          dataIndex: 'REFER_PRICE_TAXED',
          // dataIndex: 'NOT_TAX_INSTRUCT',
          sorter: true,
          filter: true,
          precision: 2,
          filterType: 'number'
        },
        {
          title: '批发价(含税)',
          dataIndex: 'COST_WHOLESALES_PRICE',
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2
        },
        {
          title: '加价率',
          dataIndex: 'PRICE_ADD_RATE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '出库价(含税)',
          dataIndex: 'COST_OUT_PRICE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '出库价1(含税)',
          dataIndex: 'COST_OUT_PRICE_A',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '出库价2(含税)',
          dataIndex: 'COST_OUT_PRICE_B',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '出库价3(含税)',
          dataIndex: 'COST_OUT_PRICE_C',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '主组号',
          dataIndex: 'MAIN_GROUP_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '类型',
          dataIndex: 'PART_TYPE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('5000'),
          dictItems: this.createDictList('5000'),
          render: params => {
            return <span>{this.createDictText(params.row.PART_TYPE, '5000')}</span>;
          }
        },
        {
          title: '车系',
          dataIndex: 'SERIES_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
          // render: params => {
          //   return <span>{this.seriesData[params.row.SERIES]}</span>;
          // }
        },
        {
          title: '销售锁',
          dataIndex: 'SALE_LOCK_MARK',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '停用标识',
          dataIndex: 'IS_DISABLE',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.IS_DISABLE, '8131')}</span>;
          },
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('8131'),
          dictItems: this.createDictList('8131')
        },
        {
          title: '停用日期',
          dataIndex: 'DISABLE_DATE',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        }
      ];
    },
    // table数据变化
    async handleTableDataChange(e, isFirst) {
      if (!this.tax) {
        let res = await queryTex();
        this.tax = res.data;
      }
      console.log(e, 'table 数据变化', isFirst);

      e.map(item => {
        item.PLAN_PRICE_TAXED = item.PLAN_PRICE * (1 + Number(this.tax ? this.tax : 0.13));
        item.COST_WHOLESALES_PRICE_TAXED = item.COST_WHOLESALES_PRICE * (1 + (Number(this.tax) || 0.13));
        item.REFER_PRICE_TAXED = item.REFER_PRICE * (1 + (Number(this.tax) || 0.13));
      });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.$refs.table.$refs.pageTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async getReceiveDetail(id) {
      const res = await getReceiveDetail(id);
      // console.log(res,'结清')
      this.showWriteOff = true;
      this.$nextTick(() => {
        this.$refs.writeOff.openModel(this.onChangeColumn, res.data);
      });
    },
    changeHandle(val) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign(
        {
          xhrAbort: false
        },
        val
      );
    },
    // 查询所有车系
    async getAllSeries() {
      let res = await getSeries();
      let obj = {};
      if (res.resultCode == 200) {
        res.data.map(item => {
          obj[item.seriesCode] = item.seriesName;
        });
      }
      this.seriesData = obj;
    }
  }
};
</script>

<style lang="less"></style>
